Skip to content

Comments

[ENH] Replace asserts with proper if else Exception handling#1589

Open
Omswastik-11 wants to merge 11 commits intoopenml:mainfrom
Omswastik-11:issue-1581
Open

[ENH] Replace asserts with proper if else Exception handling#1589
Omswastik-11 wants to merge 11 commits intoopenml:mainfrom
Omswastik-11:issue-1581

Conversation

@Omswastik-11
Copy link
Contributor

Fixes #1581

@Omswastik-11
Copy link
Contributor Author

Omswastik-11 commented Jan 2, 2026

Hi @fkiraly !! I would like to know you suggestion on this .

Screenshot 2026-01-02 133722

In openml\runs\run.py the _generate_arff_dict function is causing the above issue . so I created a new function _get_arff_attributes_for_task to reduce complexity is it ok ? or should I think of a alternate solution ?

@Omswastik-11 Omswastik-11 changed the title [MNT] Replace asserts with proper if else error handling [MNT] Replace asserts with proper if else Exception handling Jan 2, 2026
Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks reasonable for me for a first go, to isolate the if/else in a function.

More generally, from an architecture standpoint however, whenever I see a long list of if/elses on the argument class type, I think it is too high coupling and it should be either a method of the (task/argument) class or a visitor pattern.

Why: imagine you want to add a new task. Now you have to add another elif in all of these functions. This is absolutely not extensible.

This should be refactored - I would say, in another PR, after opening an issue with a plan - so the task itself, or a visitor, manages whatever is in the if/else.

@codecov-commenter
Copy link

codecov-commenter commented Jan 2, 2026

Codecov Report

❌ Patch coverage is 4.76190% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.73%. Comparing base (7feb2a3) to head (e565ca1).

Files with missing lines Patch % Lines
openml/runs/run.py 4.54% 21 Missing ⚠️
openml/runs/functions.py 7.14% 13 Missing ⚠️
openml/runs/trace.py 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1589      +/-   ##
==========================================
- Coverage   52.82%   52.73%   -0.09%     
==========================================
  Files          37       37              
  Lines        4371     4380       +9     
==========================================
+ Hits         2309     2310       +1     
- Misses       2062     2070       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fkiraly fkiraly changed the title [MNT] Replace asserts with proper if else Exception handling [ENH] Replace asserts with proper if else Exception handling Jan 2, 2026
@fkiraly fkiraly added enhancement module:Run OpenML concept labels Jan 2, 2026
@Omswastik-11 Omswastik-11 requested a review from fkiraly January 2, 2026 12:04
Copy link
Collaborator

@geetu040 geetu040 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, just ran the tests in tests/test_runs/ to be sure of the refactor, and it's green.
Ready to be merged!

Copilot AI review requested due to automatic review settings February 20, 2026 18:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces assert-based validation in the openml.runs module with explicit exceptions, improving reliability (asserts can be stripped with Python -O) and providing clearer failure modes. It also refactors ARFF attribute construction into a dedicated helper to reduce duplication.

Changes:

  • Replaced multiple assert ... is not None validations with ValueError/TypeError raises across runs-related code paths.
  • Added OpenMLRun._get_arff_attributes_for_task() and used it from _generate_arff_dict() to centralize ARFF attribute generation.
  • Tightened runtime type checks for inputs derived from task data (y type in parallel helper).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
openml/runs/trace.py Replaces asserts with explicit exceptions when parameters are unexpectedly missing.
openml/runs/run.py Adds a helper for task-dependent ARFF attributes; replaces flow-id assertion with explicit error.
openml/runs/functions.py Replaces several asserts with explicit exceptions in run/model initialization and run-list parsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if not isinstance(runs_dict["oml:runs"]["oml:run"], list):
raise TypeError(
f"Expected runs_dict['oml:runs']['oml:run'] to be a list, "
f"got {type(runs_dict['oml:runs']).__name__}"
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In __list_runs, the TypeError message reports the type of runs_dict['oml:runs'] rather than the type of runs_dict['oml:runs']['oml:run'] (the value being validated). This will mislead debugging when the XML shape is unexpected; use the nested field in the type() call so the error reflects the actual offending value.

Suggested change
f"got {type(runs_dict['oml:runs']).__name__}"
f"got {type(runs_dict['oml:runs']['oml:run']).__name__}"

Copilot uses AI. Check for mistakes.
if isinstance(task, OpenMLClusteringTask):
return [*instance_specifications, ("cluster", "NUMERIC")]

raise NotImplementedError(f"Task type {task.task_type!s} is not yet supported.")
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NotImplementedError raised for unsupported task types is less informative than the previous implementation (it no longer includes the task_id and the list of supported task types). Since this error is user-facing when uploading predictions, consider restoring the richer context (task_id, supported types, and the actual task_type value) to make troubleshooting easier.

Suggested change
raise NotImplementedError(f"Task type {task.task_type!s} is not yet supported.")
supported_task_types = [
TaskType.SUPERVISED_CLASSIFICATION,
TaskType.SUPERVISED_REGRESSION,
TaskType.CLUSTERING,
TaskType.LEARNING_CURVE,
]
raise NotImplementedError(
"Task type {task_type!s} for task_id {task_id!s} is not yet supported. "
"Supported task types are: {supported!r}".format(
task_type=task.task_type,
task_id=getattr(task, "task_id", None),
supported=supported_task_types,
)
)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MNT] Replace Assert Statements with Proper Exceptions

4 participants